﻿using AutoMapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.DepartMentes;
using Management.Systeam.Read.Apis.Application.Command.Resourceses;
using MediatR;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.Resourceses
{
    public class AddResourcesesCommandHandler : IRequestHandler<AddResourcesesCommand, ApiResult<int>>
    {
        IBaseRepository<Resources> Resources;
        IMapper mapper;

        public AddResourcesesCommandHandler(IBaseRepository<Resources> Resources, IMapper mapper)
        {
            this.Resources = Resources;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(AddResourcesesCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            try
            {
                var model = mapper.Map<Resources>(request);
                int r = Resources.InsertAsync(model).Result;
                if (r > 0)
                {
                    apiResult.Code = ApiEnum.success;
                    apiResult.Message = "添加成功";
                }
            }
            catch (Exception ex)
            {
                apiResult.Code = ApiEnum.error;
                apiResult.Message = ex.Message;
            }
            return apiResult;
        }
    }
}
